/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.registry;

import java.util.Collections;
import java.util.List;

public class DefaultTypeFactory implements TypeFactory {
	private List<Entry<?, ?>> entries = Collections.emptyList();
	
	public DefaultTypeFactory addEntries(List<Entry<?, ?>> entries) {
		this.entries = entries;

		return this;
	}

	public <B> B getInstance(Class<B> type) {
		for (Entry<?, ?> entry : entries) {
			if (entry.matches(type)) {
				return type.cast(entry.get());
			}
		}

		return null;
	}

	public <B> boolean hasInstance(Class<B> type) {
		if (entries.isEmpty()) {
			return false;
		}

		for (Entry<?, ?> entry : entries) {
			if (entry.matches(type)) {
				return true;
			}
		}

		return false;
	}
}
